package com.kaiyidiannao.model.req.model;

import android.text.TextUtils;
import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.TourBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONObject;

import java.util.HashMap;

/**
 *   凑对去页面详情-列表 异步请求
 */
public class GetTourDetail extends BaseCommand {

    private String itemid; //当前分类编号
    private String userid; //
    private String catid;

    private String TAG = "GetTourDetail";

    public GetTourDetail(String itemid) {
        this.itemid = itemid;
    }

    public GetTourDetail(String itemid, String userid, String catid) {
        this.itemid = itemid;
        this.userid = userid;
        this.catid = catid;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_TOUT_DETAIL_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("itemid", itemid);
        if (catid != null && !TextUtils.isEmpty(catid)) {
            paramters.put("catid", catid);
        }
        if (userid != null && !TextUtils.isEmpty(userid)) {
            paramters.put("userid", userid);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetTourDetail.yes()");

        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONObject o = jsonObject.optJSONObject("data");
            DataCenter.bean = null;

            TourBean bean = JsonUtil.jsonToObject(o.toString(), TourBean.class);
            DataCenter.bean = bean;

            Log.d(TAG, bean.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_TOUR_DETAIL_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetTourDetail.no()");

        DataCenter.bean = null;
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_TOUR_DETAIL_FAILED);

    }
}
